<?php

namespace common\services\umeng;

use linslin\yii2\curl\Curl;
use Yii;
use yii\base\Exception;

class UmengVerify
{
    private $host = "https://verify5.market.alicloudapi.com";
    private $verifyPath = "/api/v1/mobile/verify";

    # 友盟的APP KEY
    private $umengAppKey = '';
    # 阿里云市场
    private $aliAppKey = '';
    private $aliAppSecrety = '';

    public function __construct($terminal = '')
    {
        $this->aliAppKey = Yii::$app->params['ALIYUN_MARKET_APP_KEY'];
        $this->aliAppSecrety = Yii::$app->params['ALIYUN_MARKET_APP_SECRET'];
        if ($terminal == "android") {
            $this->umengAppKey = Yii::$app->params['UMENG_ANDROID_APP_KEY'];
        } else {
            $this->umengAppKey = Yii::$app->params['UMENG_IOS_APP_KEY'];
        }
    }


    public function setHeaders() {
        $header["Accept"] = "application/json";
        $header["Content-Type"] = "application/json; charset=UTF-8";
        $header["X-Ca-Version"] = "1";
        $header["X-Ca-Signature-Headers"] = "X-Ca-Key,X-Ca-Nonce,X-Ca-Stage,X-Ca-Timestamp,X-Ca-Version";
        $header["X-Ca-Stage"] = "RELEASE";
        $header["X-Ca-Key"] = $this->aliAppKey; //请求的阿里云AppKey
        $header["X-Ca-Timestamp"] = strval(time() * 1000);
        mt_srand((double) microtime() * 10000);
        $uuid = strtoupper(md5(uniqid(rand(), true)));
        $header["X-Ca-Nonce"] = strval($uuid);
        return $header;
    }

    public function verify($token = '', $phone = '') {
        $verify  = $this->verifyToken($token, $phone);
        return $verify;
    }

    public function verifyToken($mobile_token = '', $phone = '') {
        # 设置头部和签名
        $header = $this->setHeader();
        $header['X-Ca-Signature'] = $this->setSign($header);
        # 设置参数
        $post_data['phoneNumber'] = $phone;
        $post_data['token'] = $mobile_token;
        # 设置请求地址
        $curl_url = $this->host . $this->verifyPath . "?appkey=" . $this->umengAppKey; //appkey为用户在友盟注册的应用分配的appKey
        # 设置头部
        $headerArray = [];
        foreach ($header as $k => $v) {
            array_push($headerArray, $k . ":" . $v);
        }

        $curlClass = new Curl();

        $curlClass->setHeaders($header);
        $curlClass->setRequestBody(json_encode($post_data));

        $result = $curlClass->post($curl_url);

        return json_decode($result, true);
    }


    private function setHeader() {
        $header["Accept"] = "application/json";
        $header["Content-Type"] = "application/json; charset=UTF-8";
        $header["X-Ca-Version"] = "1";
        $header["X-Ca-Signature-Headers"] = "X-Ca-Key,X-Ca-Nonce,X-Ca-Stage,X-Ca-Timestamp,X-Ca-Version";
        $header["X-Ca-Stage"] = "RELEASE";
        $header["X-Ca-Key"] = $this->aliAppKey; //请求的阿里云AppKey

        $header["X-Ca-Timestamp"] = strval(time() * 1000);

        mt_srand((double) microtime() * 10000);
        $uuid = strtoupper(md5(uniqid(rand(), true)));
        $header["X-Ca-Nonce"] = strval($uuid);

        return $header;
    }

    private function setSign($header = []) {
        $headers = "X-Ca-Key:" . $header["X-Ca-Key"] . "\n";
        $headers .= "X-Ca-Nonce:" . $header["X-Ca-Nonce"] . "\n";
        $headers .= "X-Ca-Stage:" . $header["X-Ca-Stage"] . "\n";
        $headers .= "X-Ca-Timestamp:" . $header["X-Ca-Timestamp"] . "\n";
        $headers .= "X-Ca-Version:" . $header["X-Ca-Version"] . "\n";
        //Url
        $url = $this->verifyPath . "?appkey=" . $this->umengAppKey; //appkey为用户在友盟注册的应用分配的appKey,token和phoneNumber是app传过来的值
        $str_sign = "POST" . "\n";
        $str_sign .= $header['Accept'] . "\n";
        $str_sign .= "\n";
        $str_sign .= $header['Content-Type'] . "\n";
        $str_sign .= "\n";
        $str_sign .= $headers;
        $str_sign .= $url;
        $sign = base64_encode(hash_hmac('sha256', $str_sign, $this->aliAppSecrety, true)); //secret为APP的密钥

        return $sign;
    }
}